package com.aq.sales.business.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.aq.sales.business.intf.CommentBO;
import com.aq.sales.business.intf.OppBO;
import com.aq.sales.entity.Attachement;
import com.aq.sales.entity.Comment;
import com.aq.sales.entity.Event;
import com.aq.sales.entity.Lookup;
import com.aq.sales.entity.Opportunity;
import com.aq.sales.entity.OwnerHistory;
import com.aq.sales.entity.StateHistory;
@Service
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class OppBOImpl extends CommonBOImpl<Opportunity> implements OppBO<Opportunity> {

		@Override
	public List<Opportunity> findAll(int accId) {
		Query qry=getCurrentSession().createQuery("from Opportunity where accId=:accId");
		qry.setInteger("accId", accId);
		return qry.list();
	}
	

	@Override
	public int insert(Opportunity p) {
		String sql = "INSERT INTO OPPORTUNITY (IS_PRIVATE,NAME,AMOUNT,ACCOUNT_ID,CLOSE_DATE,NEXT_STEP,STAGE,TYPE,LEAD_SOURCE,CAMPAIGN_SOURCE,ORDER_NO,DELIVERY_STATUS,TRACKING_NO,DESCRIPTION,OWNER,CREATED_DATE,UPDATED_DATE,UPDATED_USER ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		Object[] parms=new Object[]{p.getIsPrivate(),p.getName(),p.getAmount(),p.getAccountId(),p.getCloseDate(),p.getNextStep(),p.getStage(),p.getType(),p.getLeadSource(),p.getCampaignSource(),p.getOrderNo(),p.getDeliveryStatus(),p.getTrackingNo(),p.getDescription(),p.getOwner(),p.getCreatedDate(),p.getUpdatedDate(),p.getUpdatedUser()};
		getJdbcTemplate().update(sql,parms);
		return 0;
	}
	public int insert(StateHistory p) {
		String sql="INSERT INTO STATE_HISTORY (REF_TYPE,REF_ID,STATE_ID,STATE,UPDATE_DATE,UPDATE_USER ) VALUES (?,?,?,?,?,?)";
		Object[] parms=new Object[]{p.getRefType(),p.getRefId(),p.getStateId(),"",p.getUpdateDate(),p.getUpdateUser()};
		getJdbcTemplate().update(sql,parms);
		return 0;
	}
	public int insert(OwnerHistory p) {
		String sql="INSERT INTO OWNER_HISTORY (REF_TYPE,REF_ID,OWNER_ID,NAME,UPDATE_DATE,UPDTED_USER) VALUES (?,?,?,?,?,?)";
		Object[] parms=new Object[]{p.getRefType(),p.getRefId(),p.getOwnerId(),"",p.getUpdateDate(),p.getUpdtedUser()};
		getJdbcTemplate().update(sql,parms);
		return 0;
	}
	
	
	@Transactional
	public int insert(Opportunity p,StateHistory sh,OwnerHistory oh) {
		insert(p);
		insert(sh);
		insert(oh);
		return 0;
	}
	@Override
	@Transactional
	public int update(Opportunity obj,StateHistory sh,OwnerHistory oh) {
		getCurrentSession().saveOrUpdate(obj);
		update(sh);
		update(oh);
		return 0;
	}
	public int update(StateHistory p) {
		String sql="UPDATE STATE_HISTORY SET REF_TYPE=?,REF_ID=?,STATE_ID=?,STATE=?,UPDATE_DATE=?,UPDATE_USER=? WHERE ID=? ";
		Object[] parms=new Object[]{p.getRefType(),p.getRefId(),p.getStateId(),"",p.getUpdateDate(),p.getUpdateUser(),p.getId()};
		getJdbcTemplate().update(sql,parms);
		return 0;
	}
	public int update(OwnerHistory p) {
		String sql="UPDATE OWNER_HISTORY SET REF_TYPE=?,REF_ID=?,OWNER_ID=?,NAME=?,UPDATE_DATE=?,UPDTED_USER=? WHERE ID=?";
		Object[] parms=new Object[]{p.getRefType(),p.getRefId(),p.getOwnerId(),"",p.getUpdateDate(),p.getUpdtedUser(),p.getId()};
		getJdbcTemplate().update(sql,parms);
		return 0;
	}
	@Override
	public int update(Opportunity obj) {
		getCurrentSession().update(obj);
		return 0;
	}
	@Override
	public int delete(Opportunity obj) {
		getCurrentSession().delete(obj);
		return 0;
	}

	@Override
	public Opportunity findById(int id) {
		throw new RuntimeException("This method is not Supported");
	}

	@Override
	public Opportunity findById(int id, int accId) {
		Query qry=getCurrentSession().createQuery("from Opportunity where id=:id AND accId=:accId");
		qry.setInteger("id", id);
		qry.setInteger("accId", accId);
		return (Opportunity)qry.uniqueResult();
	}
	@Override
	public List<Opportunity> query(String where,int accId) {
		String sql="from Opportunity where "+where+" AND accId=:accId";
		Query qry=getCurrentSession().createQuery(sql);
		qry.setInteger("accId", accId);
		return qry.list();
	}


	@Override
	public int addComments(Comment c) {
		CommentBO<Comment> cbo=new CommentBOImpl();
		cbo.insert(c);
		return 0;
	}


	@Override
	public int attachFile(Attachement c) {
		getCurrentSession().save(c);
		return 0;
	}


	@Override
	public int addEvent(Event c) {
		getCurrentSession().save(c);
		return 0;
	}
}
